package com.opencloud.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.opencloud.common.model.PageParams;
import com.opencloud.common.model.Result;
import com.opencloud.common.utils.StringUtils;
import com.opencloud.system.client.model.entity.SystemRole;
import com.opencloud.system.client.model.entity.SystemRoleUser;
import com.opencloud.system.client.service.ISystemRoleServiceClient;
import com.opencloud.system.service.SystemRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author liuyadu
 */
@Api(tags = "系统角色管理")
@RestController
public class SystemRoleController implements ISystemRoleServiceClient {
    @Autowired
    private SystemRoleService systemRoleService;


    @ApiOperation(value = "获取角色列表", notes = "获取角色列表")
    @GetMapping("/role")
    @Override
    public Result<Page<SystemRole>> getPage(@RequestParam Map<String, Object> map) {
        return Result.ok().data(systemRoleService.findPage(new PageParams(map)));
    }

    @ApiOperation(value = "获取角色列表", notes = "获取角色列表")
    @GetMapping("/role/list")
    @Override
    public Result<List<SystemRole>> getList() {
        return Result.ok().data(systemRoleService.findList());
    }

    @ApiOperation(value = "获取角色详情", notes = "获取角色详情")
    @GetMapping("/role/info")
    @Override
    public Result<SystemRole> get(@RequestParam(value = "roleId") Long roleId) {
        SystemRole result = systemRoleService.getById(roleId);
        return Result.ok().data(result);
    }

    @ApiOperation(value = "添加/修改角色", notes = "添加/修改角色")
    @PostMapping("/role/save")
    @Override
    public Result<Long> save(@RequestBody SystemRole systemRole) {
        if (systemRole.getRoleId() == null) {
            systemRoleService.add(systemRole);
        } else {
            systemRoleService.update(systemRole);
        }
        return Result.ok();
    }

    @ApiOperation(value = "删除角色", notes = "删除角色")
    @PostMapping("/role/remove")
    @Override
    public Result remove(@RequestParam(value = "roleId") Long roleId) {
        systemRoleService.remove(roleId);
        return Result.ok();
    }

    @ApiOperation(value = "角色添加成员列表", notes = "角色添加成员列表")
    @PostMapping("/role/save/users")
    @Override
    public Result saveUsers(
            @RequestParam(value = "roleId") Long roleId,
            @RequestParam(value = "userIds", required = false) String userIds
    ) {
        systemRoleService.saveUsers(roleId, StringUtils.isNotBlank(userIds) ? userIds.split(",") : new String[]{});
        return Result.ok();
    }

    @ApiOperation(value = "获取角色成员列表", notes = "获取角色成员列表")
    @GetMapping("/role/users")
    @Override
    public Result<List<SystemRoleUser>> getRoleUsers(@RequestParam(value = "roleId") Long roleId) {
        return Result.ok().data(systemRoleService.findUsersByRoleId(roleId));
    }

}
